<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/30
 * Time: 9:13
 */

namespace app\api\controller;


use mikkle\tp_wechat\support\Db;
use think\Controller;
header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");
class Order extends Controller{

    private $user;
    public function _initialize(){

        parent::_initialize();
        $map = array(
            'username' => input('username', ''),
            'password' => input('password', ''),
            'login_key' => input('login_key', ''),
        );
        $this->user = Db::name('username')->where($map)->where('open', '<', 1)->find();
        //验证加密
        $VALUE_KEY = ['afewfewfew21342dsaewf'=>'8f4da4f9ew4f98ewq65fwe48f9w'];
        $HEADER_TIME = isset($_SERVER['HTTP_TIME']) ? $_SERVER['HTTP_TIME'] : '';
        $HEADER_KEY = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : '';
        $HEADER_SIGN = isset($_SERVER['HTTP_SIGN']) ? $_SERVER['HTTP_SIGN'] : '';

        $POST_LOGIN_KEY = isset($_SERVER['HTTP_LOGINKEY']) ? $_SERVER['HTTP_LOGINKEY'] : ( isset($_POST['login_key']) ? $_POST['login_key'] : '' );
        $POST_NAME = isset($_SERVER['HTTP_USERNAME']) ? $_SERVER['HTTP_USERNAME'] : (isset($_POST['username']) ? $_POST['username'] : (isset($_POST['name']) ? $_POST['name']:''));



        if( !isset($VALUE_KEY[$HEADER_KEY]) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }

        $validation =  $HEADER_TIME.$VALUE_KEY[$HEADER_KEY].$POST_LOGIN_KEY.$POST_NAME;
        if( $HEADER_SIGN != md5($validation) ){
            exit(json_encode(['result'=>0], JSON_UNESCAPED_UNICODE));
        }
    }

    public function category(){
        $caizhong = Db::name('caipai_list')->where('cp_if_open',1)->field('id,cp_name_old,cp_name_new')->select();
        $ret = array(
            'result'=>1,
            'ResultMsg'=>'',
            'datas' => []
        );
        foreach($caizhong as $v){
            $ret['datas'][] = array(
                'id' => $v['id'],
                'cp_name' => !empty($v['cp_name_new']) ? $v['cp_name_new'] : $v['cp_name_old'],
            );
        }
        echo json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
    public function orderList(){
        if( empty($this->user) ){
            $ret = array(
                'result'=>0,
                'ResultMsg'=>'用户信息错误'
            );
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $post = input('post.');
        $game_id = isset($post['game_id']) ? intval($post['game_id']) : 0;
        // 订单类型 0：全部；1：已中奖；2:待开奖；3：追号订单
        $type = isset($post['type']) ? intval($post['type']) : 0;
        $page = isset($post['page']) ? intval($post['page']) : 1;
        //查询时间端 0：今天；1：昨天；2：本周；3：本月；4：上月
        $range = isset($post['range']) ? intval($post['range']) : 0;
        $username = isset($post['username']) ? $post['username'] : '';
        $limit = 10;
        $ret = array(
            'result'=>0,
            'ResultMsg'=>'无信息',
        );
        if( !empty($username) ){
            $where['username'] = $username;
            if( 0!=$game_id ) $where['czid'] = $game_id;
            switch( $range ){
                case 1:
                    $begin = strtotime( date("Y-m-d 00:00:00",strtotime("-1 day")) );
                    $end = strtotime( date("Y-m-d 23:59:59",strtotime("-1 day")) );
                    break;
                case 2:
                    $begin = mktime( 0, 0 , 0, date("m"), date("d")-date("w")+1, date("Y") );
                    $end = mktime( 23,59,59,date("m"), date("d")-date("w")+7, date("Y") );
                    break;
                case 3:
                    $begin = mktime(0, 0 , 0,date("m"),1,date("Y"));
                    $end = mktime(23,59,59,date("m"),date("t"),date("Y"));
                    break;
                case 4:
                    $begin = mktime(0, 0 , 0,date("m")-1,1,date("Y"));
                    $end = mktime(23,59,59,date("m") ,0,date("Y"));
                    break;
                default:
                    $begin = strtotime(date("Y-m-d 00:00:00",time()));
                    $end = strtotime(date("Y-m-d 23:59:59",time()));

            }
            $where['tz_time'] = ['between',[$begin,$end]];

            $list = array();
            $sw = Db::name("username")->where(['username'=>$username])->value("swzh");
            $nbzh = Db::name("username")->where(['username'=>$username])->value("nbzh");
            1==intval($nbzh) && $sw = 1;
            switch( $type ){
                case 0:
                    $listobj = Db::field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,1 as type')
                        ->table('kkty_touzhu_list_no')
                        ->where($where);
                    if( 1==intval( $sw ) ){
                        $listobj->union(function($query) use ($where){
                            $query->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,2 as type')->table('kkty_touzhu_list_sw')->where($where);
                        });
                    }else{
                        $listobj->union(function($query) use ($where){
                            $query->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,3 as type')->table('kkty_touzhu_list_ok')->where($where);
                        });
                    }
                    $list = $listobj->union(function($query) use ($where){
                        $query->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,4 as type')->table('kkty_touzhu_list_zh')->where($where);
                    })->select();
                    break;
                case 1:
                    if( 1==intval( $sw ) ){
                        $list = Db::name('touzhu_list_sw')->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,2 as type')->where($where)->where(['zjje'=>['>',0]])->order("tz_time desc")->page($page,$limit)->select();
                    }else{
                        $list = Db::name('touzhu_list_ok')->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,3 as type')->where($where)->where(['zjje'=>['>',0]])->order("tz_time desc")->page($page,$limit)->select();
                    }
                    break;
                case 2:
                    $list = Db::name('touzhu_list_no')->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,1 as type')->where($where)->order("tz_time desc")->page($page,$limit)->select();
                    break;
                case 3:
                    $list = Db::name('touzhu_list_zh')->field('id,czid,qihao,tzhm,tzje,username,zjje,kjjg,tz_time,4 as type')->where($where)->order("tz_time desc")->page($page,$limit)->select();
                    break;
            }
            $caizhong = Db::name('caipai_list')->column('id,logo,cp_name_old,cp_name_new');
            $orderKey = array();
            $config = config("lotterys");
            foreach( $list as &$v ){
                $orderKey[] = intval($v['tz_time']);
                $v['cp_name'] = isset($caizhong[$v['czid']]) ? (!empty($caizhong[$v['czid']]['cp_name_new']) ? $caizhong[$v['czid']]['cp_name_new'] : $caizhong[$v['czid']]['cp_name_old']) : '';
                $v['logo'] = isset($caizhong[$v['czid']]) ? $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$caizhong[$v['czid']]['logo'] : '';
                $v['status'] = ( '0'==$v['kjjg'] ) ? 0 : ( floatval($v['zjje'])>0 ? 1 : 2 );
                foreach($config as $k=>$val){
                    in_array($v['czid'],$val) && $v['abc'] = $k;
                }
            }
            unset($v);
            array_multisort($orderKey,SORT_DESC,SORT_NUMERIC,$list);
            if( 0 == $type ){
                $list = array_slice($list,intval(($page-1) * $limit),intval($page*$limit));
            }
            $ret = array(
                'result'=>1,
                'ResultMsg'=>'',
                'datas' => $list
            );
        }
        echo json_encode($ret,JSON_UNESCAPED_UNICODE);
    }

    public function orderDetail(){

        // 订单类型 1：no；2:sw；3：ok,4:zh

        $id = intval( input('post.id') );
        $from = intval( input('post.type') );
        if( 1==$from ){
            $table = 'touzhu_list_no';
        }elseif( 2==$from ){
            $table = 'touzhu_list_sw';
        }elseif( 3==$from ){
            $table = 'touzhu_list_ok';
        }else{
            $table = 'touzhu_list_zh';
        }
        //  订单详情

        $detail = Db::name($table)->alias('a')
            ->join("__CAIPAI_LIST__ b",'a.czid=b.id')
            ->join("__PLAY_CLASSIFY__ c",'a.wfid=c.id')
            ->field('a.id,a.czid,b.cp_name_old,b.cp_name_new,a.qihao,a.tzhm,c.class_name_mini,c.digit,a.tzje,a.dzje,a.nbzh,a.dqpl,a.kjjg,a.zjje,a.tz_time,a.wfid')
            ->where(['a.id'=>$id])
            ->find();
        if( empty($detail) ){
            $ret = array(
                'result'=>0,
                'ResultMsg'=>'',
            );
        }else{
            //4:追号，待开奖；3：待开奖;2；中奖；1：未中奖
            $detail['status'] = ( '0'==$detail['kjjg'] ) ? ((3==$from) ? 4 : 3) : ( (floatval($detail['zjje'])>0) ? 2:1 );
            $detail['tzhmgs'] = !empty($detail['digit']) ? digit($detail['tzhm'],$detail['digit'],$detail['czid'],$detail['wfid']) : $detail['tzhm'];
            $detail['cp_name'] = !empty($detail['cp_name_new']) ? $detail['cp_name_new'] : $detail['cp_name_old'];
            if( !empty($detail['kjjg']) ) {
                $detail['kjjg'] = kjjg_split($detail['kjjg'], $detail['czid']);
                $detail['kjjg'] = implode(' ',$detail['kjjg']);
            }
            $ret = array(
                'result'=>1,
                'ResultMsg'=>'',
                'datas' => $detail
            );
        }
        echo json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
}